package com.yfdice.android.bam;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.*;
import android.content.*;

/*
 * <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

system/media/audio/alarms
 */
public class Settings extends Activity implements SeekBar.OnSeekBarChangeListener
{
	private SharedPreferences mSettings;
	private AudioManager mAudioMgr = null;
	private SeekBar mSeekBar;
	private TextView mProgressText;
	private int mProgress = 0;
	private CheckBox mAutostartCheck;
	private CheckBox mSilenceAlarmCheck;
	private CheckBox mServiceNotifCheck;
	private EditText mSearchTimeEdit;
	private EditText mRSSIEdit;
	
	public static final String PREFS_NAME = "BAMSettings"; 
	public static final String AUTOSTART_NAME = "autostart";
	public static final String SILENCE_NAME = "silencealarm";
	public static final String NOTIFICATION_NAME = "notification";
	public static final String SEARCHTIME_NAME = "searchtime";
	public static final String RSSI_NAME = "maxrssi";
	
	public static final int def_MAX_RSSI = 80;
	
	//@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		this.bindService( new Intent( "com.yfdice.android.bam.BAMService" ),
                this.mServiceConnection, BIND_AUTO_CREATE);
		
		mSettings = getSharedPreferences(PREFS_NAME, 0);
		mSeekBar = (SeekBar)findViewById(R.id.seek);
		mSeekBar.setOnSeekBarChangeListener(this);
		
		mProgressText = (TextView)findViewById(R.id.settings_volumn);
		mAutostartCheck = (CheckBox)findViewById(R.id.autostart_checkbox);
		mAutostartCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {  
            //@Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {  
            	if (mSettings != null)
            	{
            		SharedPreferences.Editor editor = mSettings.edit();  
            		editor.putBoolean(AUTOSTART_NAME, isChecked);  
            		editor.commit();
            	}
            }  
        } );
		
		mSilenceAlarmCheck = (CheckBox)findViewById(R.id.silencealarm_checkbox);
		mSilenceAlarmCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {  
            //@Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {  
            	if (mSettings != null)
            	{
            		SharedPreferences.Editor editor = mSettings.edit();  
            		editor.putBoolean(SILENCE_NAME, isChecked);  
            		editor.commit();
            	}
            }  
        } );
		
		mServiceNotifCheck = (CheckBox)findViewById(R.id.servicenotification_checkbox);
		mServiceNotifCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {  
            //@Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {  
            	if (mSettings != null)
            	{
            		SharedPreferences.Editor editor = mSettings.edit();  
            		editor.putBoolean(NOTIFICATION_NAME, isChecked);  
            		editor.commit();
            	}
            }  
        } );
		
		mSearchTimeEdit = (EditText)findViewById(R.id.bluetoothsearch_editbox);
		mRSSIEdit = (EditText)findViewById(R.id.bluetoothrssi_editbox);
		
		initData();
	}
	
	public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2)
	{
		mProgress = arg1;
		mProgressText.setText(String.valueOf(mProgress));
	}
	  
	 // Method descriptor #6 (Landroid/widget/SeekBar;)V
	 public void onStartTrackingTouch(SeekBar arg0)
	 {
		 
	 }
	  
	 // Method descriptor #6 (Landroid/widget/SeekBar;)V
	 public void onStopTrackingTouch(SeekBar arg0)
	 {
		 mAudioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, mProgress, 0);
	 }
	
	 private void initData()
	 {
		 mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		 
		 // 获取系统当前音量
		 mProgress = mAudioMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
		 mSeekBar.setProgress(mProgress);
		 mProgressText.setText(String.valueOf(mProgress));
		 
		 boolean bStatue = mSettings.getBoolean(AUTOSTART_NAME, true); 
		 mAutostartCheck.setChecked(bStatue);
		 
		 bStatue = mSettings.getBoolean(SILENCE_NAME, true); 
		 mSilenceAlarmCheck.setChecked(bStatue);
		 
		 bStatue = mSettings.getBoolean(NOTIFICATION_NAME, true); 
		 mServiceNotifCheck.setChecked(bStatue);
		 
		 int nValue = mSettings.getInt(SEARCHTIME_NAME, 1000);
		 mSearchTimeEdit.setText(String.valueOf(nValue / 1000));
		 
		 nValue = mSettings.getInt(RSSI_NAME, def_MAX_RSSI);
		 mRSSIEdit.setText(String.valueOf(nValue));
	 }

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		SharedPreferences.Editor editor = mSettings.edit();
		int nValue = Integer.parseInt(mSearchTimeEdit.getText().toString());
		nValue *= 1000; // 转换为毫秒
		editor.putInt(SEARCHTIME_NAME, nValue); 
		int nValue2 = Integer.parseInt(mRSSIEdit.getText().toString());
		editor.putInt(RSSI_NAME, nValue2); 
		editor.commit();
		
		MoniterService.getInstance().setScanSchedule(nValue);
		MoniterService.getInstance().setWarningRSSI(nValue2);
		
		this.unbindService(mServiceConnection);
		super.onDestroy();
	}
	
	private IBAMService mBAMService = null;
	private ServiceConnection mServiceConnection = new ServiceConnection() 
	{
        //@Override
        public void onServiceConnected(ComponentName name, IBinder service) 
        {
        	mBAMService = (IBAMService) service;
            //Log.i(TAG, "BAMService on serivce connected");
        }

        //@Override
        public void onServiceDisconnected(ComponentName name) 
        {
        	mBAMService = null ;
        }

    };
	 
}